﻿using System.Linq;
using EasySite.Services;
using NUnit.Framework;

namespace EasySite.Tests.ServiceTests
{
    public class MenuServiceTests : DbUnitTestBase
    {
        [Test]
        public void Should_return_menus()
        {
            var service = base.GetService<IMenuService>();
            var menus = service.GetMenuItems();
            Assert.IsNotNull(menus);
        }

        [Test]
        public void Shouldnt_return_deleted()
        {
            var service = base.GetService<IMenuService>();
            var menus = service.GetMenuItems();
            Assert.IsTrue(!menus.Any(m => m.Deleted));
        }

        [Test]
        public void Should_orderby_sequence()
        {
            var service = base.GetService<IMenuService>();
            var menus = service.GetMenuItems();
            if (menus.Count > 1)
            {
                for (var i = 0; i < menus.Count - 1; i++)
                {
                    Assert.IsTrue(menus[i].Sequence < menus[i + 1].Sequence);
                }
            }
            foreach (var menu in menus)
            {
                var tempMenu = menu;
                while (true)
                {
                    if (tempMenu.Children.Count == 0)
                    {
                        break;
                    }
                    if (tempMenu.Children.Count > 1)
                    {
                        for (var i = 0; i < tempMenu.Children.Count - 1; i++)
                        {
                            Assert.IsTrue(tempMenu.Children[i].Sequence < tempMenu.Children[i + 1].Sequence);
                        }
                    }
                    foreach (var child in tempMenu.Children)
                    {
                        tempMenu = child;
                    }
                }
            }
        }

        [Test]
        public void Should_have_submenus()
        {
            var service = base.GetService<IMenuService>();
            var menus = service.GetMenuItems();
            Assert.IsTrue(menus.First(m => m.Name == "Dairies").Children.Count == 2);
        }

        [Test]
        public void Should_have_roles()
        {
            var service = base.GetService<IMenuService>();
            var menus = service.GetMenuItems();
            var dairyRoles = menus.First(m => m.Name == "Dairies").Roles;
            Assert.IsTrue(dairyRoles.Contains("Admin"));
            Assert.IsTrue(dairyRoles.Contains("Friend"));
            Assert.IsTrue(menus.First(m => m.Name == "Admin").Roles[0] == "Admin");
        }
    }
}
